package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;

import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.UserServic;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags="用户管理接口")
@RequestMapping("/users")
@RestController
@RequiredArgsConstructor
public class Usercontroller {

    private final UserServic userServic;

    @PostMapping()
    @ApiOperation("新增用户")
    public void saveUser(@RequestBody UserFormDTO userFormDTO){

        User user= BeanUtil.copyProperties(userFormDTO, User.class);
        userServic.save(user);

    }

    @GetMapping("{id}")
    @ApiOperation("根据id查询用户接口")
    public UserVO queryUserById(@ApiParam("用户id") @PathVariable long id){
        return userServic.queryUserAndAddressById(id);
    }

    @ApiOperation("扣减用户余额接口")
    @PutMapping("/{id}/deduction/{money}")
    public void deductMoneyById(
            @ApiParam("用户id") @PathVariable("id") Long id,
            @ApiParam("扣减的金额") @PathVariable("money") Integer money){
        userServic.deductMoney(id,money);
    }
    @ApiOperation("根据复杂条件查询用户接口")
    @GetMapping("/list")
    public List<UserVO> queryUsers(UserQuery query){
        List<User> users=userServic.queryUsers(query.getName(), query.getStatus(), query.getMinBalance(), query.getMaxBalance());
        return BeanUtil.copyToList(users, UserVO.class);
    }
    @ApiOperation("根据id条件查询用户接口")
    @GetMapping
    public List<UserVO> queryUserByIds(@ApiParam ("用户id集合")@RequestParam List<Long> ids){
        return userServic.queryUsersAndAddressByIds(ids);
    }

}
